home *** CD-ROM | disk | FTP | other *** search
/ MIDICraft's MIDINET CD-ROM / MIDICraft's MIDINET CD-ROM.iso / DOSUTILS / KORGI3 / HEARI3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-17  |  3.0 KB  |  173 lines

  1. // heari3: see if korg is responding
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include "sb.hpp"
  8. #include "mpu.hpp"
  9. #include <stdlib.h>
  10.  
  11. Soundcard* card = 0;
  12. int copy = 0;
  13. int dump = 1;
  14.  
  15. unsigned char mapchannel[16] = {
  16.   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
  17. };
  18.  
  19. unsigned char ignorelow[16] = {
  20.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  21. };
  22.  
  23. #define ISLOW(note)  (note <= 0x36)
  24.  
  25. int hear()
  26. {
  27.   card->reset();
  28.   unsigned char c;
  29.   int ret = 0;
  30.   int col = 0;
  31.   unsigned char checklow = 0;
  32.  
  33.   fprintf(stderr, "send midi commands from external keyboard:\n");
  34.   card->startinput();
  35.   while (!kbhit() || getch() != 27)
  36.   {
  37.     int count = card->hear(&c, 1);
  38.     if (count <= 0)
  39.       continue;
  40.     ret = 1;
  41.     if (c == 0xFE)
  42.     {
  43.       if (dump)
  44.       {
  45.     putchar('°');
  46.     col++;
  47.       }
  48.     }
  49.     else if (c == 0xF8)
  50.     {
  51.       if (dump)
  52.       {
  53.     putchar('.');  // tick
  54.     col++;
  55.       }
  56.     }
  57.     else
  58.     {
  59.       if (copy)
  60.       {
  61.     if (checklow)
  62.     {
  63.       if (ISLOW(c))
  64.       {
  65.         checklow = (checklow & 15) + 0x80; // note off
  66.         c = 0;
  67.       }
  68.       card->play(&checklow, 1);
  69.     }
  70.     checklow = 0;
  71.     if (c & 0x80 && c < 0xF0)
  72.     {
  73.       int ch = c & 15;
  74.       if (mapchannel[ch] != c)
  75.       {
  76.         c = (c & 0xF0) + mapchannel[ch];
  77.         ch = c & 15;
  78.       }
  79.       if ((c & 0x80) <= 0x90 && ignorelow[ch])
  80.       {
  81.         checklow = c;
  82.       }
  83.     }
  84.     card->play(&c, 1);
  85.       }
  86.       if (dump)
  87.       {
  88.     if (c & 0x80)
  89.     {
  90.       if (dump)
  91.         putchar('\n');
  92.       col = 0;
  93.     }
  94.     else
  95.     {
  96.       if (dump)
  97.         putchar(' ');
  98.       col++;
  99.     }
  100.     printf("%02X", c);
  101.     col += 2;
  102.       }
  103.     }
  104.     if (dump && col >= 75)
  105.     {
  106.       putchar('\n');
  107.       col = 0;
  108.     }
  109.   }
  110.   card->stopinput();
  111.   return ret;
  112. }
  113.  
  114.  
  115. int main(int argc, char** argv)
  116. {
  117.   argc--; argv++;
  118.   while (argc > 0 && **argv == '-')
  119.   {
  120.     if (strnicmp(*argv, "-help", 2) == 0)
  121.     {
  122.       fprintf(stderr, "usage: heari3 [-copy] [-nodump] [-drum #]\n");
  123.       return 1;
  124.     }
  125.     if (strnicmp(*argv, "-copy", 2) == 0)
  126.     {
  127.       copy = 1;
  128.       argc--; argv++; continue;
  129.     }
  130.     if (strnicmp(*argv, "-drum", 2) == 0)
  131.     {
  132.       argc--; argv++;
  133.       if (argc > 0)
  134.       {
  135.     int ch = atoi(*argv); argc--; argv++;
  136.     if (ch >= 1 && ch <= 16)
  137.       mapchannel[ch-1] = 9;
  138.       }
  139.       continue;
  140.     }
  141.     if (strnicmp(*argv, "-nodump", 2) == 0)
  142.     {
  143.       dump = 0;
  144.       argc--; argv++; continue;
  145.     }
  146.     if (strnicmp(*argv, "-ignorelow", 2) == 0)
  147.     {
  148.       argc--; argv++;
  149.       if (argc > 0)
  150.       {
  151.     int ch = atoi(*argv); argc--; argv++;
  152.     if (ch >= 1 && ch <= 16)
  153.       ignorelow[ch-1] = 1;
  154.       }
  155.       continue;
  156.     }
  157.     fprintf(stderr, "invalid option %s\n", *argv);
  158.     argc--; argv++;
  159.   }
  160.   card = detect_soundcard();
  161.   if (!card)
  162.   {
  163.     fprintf(stderr, "Could not detect soundcard\n");
  164.     return 1;
  165.   }
  166.  
  167.   if (!hear())
  168.     fprintf(stderr, "no response from soundcard\n");
  169.  
  170.   delete card;
  171.   return 0;
  172. }
  173.